package cn.com.css.taxInspection.gt3.service.impl;

import cn.com.css.taxInspection.gt3.entity.ZxhjByqlqsqktjEntity;
import cn.com.css.taxInspection.gt3.entity.ZxhjByqlqsqktjRglrEntity;
import cn.com.css.taxInspection.gt3.mapper.ZxhjByqlqsqktjMapper;
import cn.com.css.taxInspection.gt3.mapper.ZxhjByqlqsqktjRglrMapper;
import cn.com.css.taxInspection.gt3.service.ZxhjByqlqsqktjService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("ZxhjByqlqsqktjService")
public class ZxhjByqlqsqktjServiceImpl extends ServiceImpl<ZxhjByqlqsqktjMapper, ZxhjByqlqsqktjEntity> implements ZxhjByqlqsqktjService {


    @Resource
    private ZxhjByqlqsqktjMapper zxhjByqlqsqktjMapper;

    @Resource
    private ZxhjByqlqsqktjRglrMapper  zxhjByqlqsqktjRglrMapper;

    @Override
    public IPage<ZxhjByqlqsqktjEntity> queryZxhjByqlqsqktjTable(ZxhjByqlqsqktjEntity zxhjByqlqsqktjEntity) {
        Page<ZxhjByqlqsqktjEntity> page = new Page<>(zxhjByqlqsqktjEntity.getPage(), zxhjByqlqsqktjEntity.getLimit());
        LambdaQueryWrapper<ZxhjByqlqsqktjEntity> queryWrapper = Wrappers.lambdaQuery();
        //案源编号
        if (TsBqUtils.isNotNull(zxhjByqlqsqktjEntity.getRkxhrq())){
            queryWrapper.eq(ZxhjByqlqsqktjEntity::getRkxhrq, zxhjByqlqsqktjEntity.getRkxhrq());
        }
        //案源编号
        if (TsBqUtils.isNotNull(zxhjByqlqsqktjEntity.getJcajbh())) {
            queryWrapper.like(ZxhjByqlqsqktjEntity::getJcajbh, zxhjByqlqsqktjEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(zxhjByqlqsqktjEntity.getNsrsbh())) {
            queryWrapper.like(ZxhjByqlqsqktjEntity::getNsrsbh, zxhjByqlqsqktjEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(zxhjByqlqsqktjEntity.getNsrmc())) {
            queryWrapper.like(ZxhjByqlqsqktjEntity::getNsrmc, zxhjByqlqsqktjEntity.getNsrmc());
        }
        //案件所在税务机关
        if (TsBqUtils.isNotNull(zxhjByqlqsqktjEntity.getAjszjgDm())) {
            queryWrapper.eq(ZxhjByqlqsqktjEntity::getAjszjgDm, zxhjByqlqsqktjEntity.getAjszjgDm());
        }
        queryWrapper.and(wp->wp.ne(ZxhjByqlqsqktjEntity::getQlsk,0)
                .or().ne(ZxhjByqlqsqktjEntity::getQlfk,0)
                .or().ne(ZxhjByqlqsqktjEntity::getQlznj,0)
                .or().ne(ZxhjByqlqsqktjEntity::getQjsk,0)
                .or().ne(ZxhjByqlqsqktjEntity::getQjfk,0)
                .or().ne(ZxhjByqlqsqktjEntity::getQjznj,0)
        );
//        LambdaQueryWrapper<ZxhjByqlqsqktjEntity> queryWrapper1 = Wrappers.lambdaQuery();
//        queryWrapper1.eq(ZxhjByqlqsqktjEntity::getSjsk,0);
//        queryWrapper1.eq(ZxhjByqlqsqktjEntity::getSjfk,0);
//        queryWrapper1.eq(ZxhjByqlqsqktjEntity::getSjznj,0);
//        queryWrapper1.eq(ZxhjByqlqsqktjEntity::getQjsk,0);
//        queryWrapper1.eq(ZxhjByqlqsqktjEntity::getQjfk,0);
//        queryWrapper1.eq(ZxhjByqlqsqktjEntity::getQjznj,0);
//        List<ZxhjByqlqsqktjEntity> zxhjByqlqsqktjEntities = zxhjByqlqsqktjMapper.selectList(queryWrapper1);
//        if (zxhjByqlqsqktjEntities != null && zxhjByqlqsqktjEntities.size()>0) {
//            List<String> uuids = new ArrayList<>();
//            zxhjByqlqsqktjEntities.forEach(i->{
//                uuids.add(i.getUuid());
//            });
//            queryWrapper.notIn(ZxhjByqlqsqktjEntity::getUuid,uuids);
//        }
        queryWrapper.orderByDesc(ZxhjByqlqsqktjEntity::getJcajbh);
        Page<ZxhjByqlqsqktjEntity> page1 = this.page(page, queryWrapper);
        List<ZxhjByqlqsqktjEntity> records = page1.getRecords();
        records.forEach(i->{
            LambdaQueryWrapper<ZxhjByqlqsqktjRglrEntity> queryWrapper1 = Wrappers.lambdaQuery();
            if (i.getJcajxxuuid() == null) {
                queryWrapper1.isNull(ZxhjByqlqsqktjRglrEntity::getJcajxxuuid);
            }else {
                queryWrapper1.eq(ZxhjByqlqsqktjRglrEntity::getJcajxxuuid, i.getJcajxxuuid());
            }
            if (i.getNsrsbh() == null) {
                queryWrapper1.isNull(ZxhjByqlqsqktjRglrEntity::getNsrsbh);
            }else {
                queryWrapper1.eq(ZxhjByqlqsqktjRglrEntity::getNsrsbh, i.getNsrsbh());
            }
            if (i.getNsrmc() == null) {
                queryWrapper1.isNull(ZxhjByqlqsqktjRglrEntity::getNsrmc);
            }else {
                queryWrapper1.eq(ZxhjByqlqsqktjRglrEntity::getNsrmc, i.getNsrmc());
            }
            if (i.getJcajbh() == null) {
                queryWrapper1.isNull(ZxhjByqlqsqktjRglrEntity::getJcajbh);
            }else {
                queryWrapper1.eq(ZxhjByqlqsqktjRglrEntity::getJcajbh, i.getJcajbh());
            }
            if (i.getZxry() == null) {
                queryWrapper1.isNull(ZxhjByqlqsqktjRglrEntity::getZxry);
            }else {
                queryWrapper1.eq(ZxhjByqlqsqktjRglrEntity::getZxry, i.getZxry());
            }
            if (i.getRkxhrq() == null) {
                queryWrapper1.isNull(ZxhjByqlqsqktjRglrEntity::getRkxhrq);
            }else {
                queryWrapper1.eq(ZxhjByqlqsqktjRglrEntity::getRkxhrq, i.getRkxhrq());
            }
            if (i.getAjszjgDm() == null) {
                queryWrapper1.isNull(ZxhjByqlqsqktjRglrEntity::getAjszjgDm);
            }else {
                queryWrapper1.eq(ZxhjByqlqsqktjRglrEntity::getAjszjgDm, i.getAjszjgDm());
            }
            ZxhjByqlqsqktjRglrEntity ZxhjByqlqsqktjRglrEntity1 = zxhjByqlqsqktjRglrMapper.selectOne(queryWrapper1);
            if (ZxhjByqlqsqktjRglrEntity1 != null) {
                i.setSfwdxdpsj(ZxhjByqlqsqktjRglrEntity1.getSfwdxdpsj());
                i.setBz(ZxhjByqlqsqktjRglrEntity1.getBz());
            }
        });
        page1.setRecords(records);
        return page1;
    }

    @Override
    public Boolean updateQlqs(ZxhjByqlqsqktjEntity zxhjByqlqsqktjEntity) {
        return this.updateById(zxhjByqlqsqktjEntity);
    }

    @Override
    public Boolean insertZxtzByqlqqqktjbRglr(ZxhjByqlqsqktjEntity zxhjByqlqsqktjEntity) {
        if ( zxhjByqlqsqktjEntity.getJcajxxuuid().equals("")){
            zxhjByqlqsqktjEntity.setJcajxxuuid(null);
        }
        if ( zxhjByqlqsqktjEntity.getNsrsbh().equals("")){
            zxhjByqlqsqktjEntity.setNsrsbh(null);
        }        if ( zxhjByqlqsqktjEntity.getNsrmc().equals("")){
            zxhjByqlqsqktjEntity.setNsrmc(null);
        }        if ( zxhjByqlqsqktjEntity.getJcajbh().equals("")){
            zxhjByqlqsqktjEntity.setJcajbh(null);
        }       if ( zxhjByqlqsqktjEntity.getZxry().equals("")){
            zxhjByqlqsqktjEntity.setZxry(null);
        } if ( zxhjByqlqsqktjEntity.getAjszjgDm().equals("")){
            zxhjByqlqsqktjEntity.setAjszjgDm(null);
        }
        if ( zxhjByqlqsqktjEntity.getRkxhrq().equals("")){
            zxhjByqlqsqktjEntity.setRkxhrq(null);
        }
        LambdaQueryWrapper<ZxhjByqlqsqktjRglrEntity> queryWrapper = Wrappers.lambdaQuery();
        if (zxhjByqlqsqktjEntity.getJcajxxuuid() == null) {
            queryWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getJcajxxuuid);
        }else {
            queryWrapper.eq(ZxhjByqlqsqktjRglrEntity::getJcajxxuuid, zxhjByqlqsqktjEntity.getJcajxxuuid());
        }
        if (zxhjByqlqsqktjEntity.getNsrsbh() == null) {
            queryWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getNsrsbh);
        }else {
            queryWrapper.eq(ZxhjByqlqsqktjRglrEntity::getNsrsbh, zxhjByqlqsqktjEntity.getNsrsbh());
        }
        if (zxhjByqlqsqktjEntity.getNsrmc() == null) {
            queryWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getNsrmc);
        }else {
            queryWrapper.eq(ZxhjByqlqsqktjRglrEntity::getNsrmc, zxhjByqlqsqktjEntity.getNsrmc());
        }
        if (zxhjByqlqsqktjEntity.getJcajbh() == null) {
            queryWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getJcajbh);
        }else {
            queryWrapper.eq(ZxhjByqlqsqktjRglrEntity::getJcajbh, zxhjByqlqsqktjEntity.getJcajbh());
        }
        if (zxhjByqlqsqktjEntity.getZxry() == null) {
            queryWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getZxry);
        }else {
            queryWrapper.eq(ZxhjByqlqsqktjRglrEntity::getZxry, zxhjByqlqsqktjEntity.getZxry());
        }
        if (zxhjByqlqsqktjEntity.getRkxhrq() == null) {
            queryWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getRkxhrq);
        }else {
            queryWrapper.eq(ZxhjByqlqsqktjRglrEntity::getRkxhrq, zxhjByqlqsqktjEntity.getRkxhrq());
        }

        if (zxhjByqlqsqktjEntity.getAjszjgDm() == null) {
            queryWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getAjszjgDm);
        }else {
            queryWrapper.eq(ZxhjByqlqsqktjRglrEntity::getAjszjgDm, zxhjByqlqsqktjEntity.getAjszjgDm());
        }
        ZxhjByqlqsqktjRglrEntity ZxhjByqlqsqktjRglrEntity1 = zxhjByqlqsqktjRglrMapper.selectOne(queryWrapper);
        if (ZxhjByqlqsqktjRglrEntity1 == null) {
            ZxhjByqlqsqktjRglrEntity ZxhjByqlqsqktjRglrEntity = new ZxhjByqlqsqktjRglrEntity();
            BeanUtils.copyProperties(zxhjByqlqsqktjEntity, ZxhjByqlqsqktjRglrEntity);
            int insert = zxhjByqlqsqktjRglrMapper.insert(ZxhjByqlqsqktjRglrEntity);
            if (insert == 1) {
                return true;
            } else {
                return false;
            }
        } else {
            LambdaUpdateWrapper<ZxhjByqlqsqktjRglrEntity> updateWrapper = Wrappers.lambdaUpdate();
            if (zxhjByqlqsqktjEntity.getJcajxxuuid() == null) {
                updateWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getJcajxxuuid);
            }else {
                updateWrapper.eq(ZxhjByqlqsqktjRglrEntity::getJcajxxuuid, zxhjByqlqsqktjEntity.getJcajxxuuid());
            }
            if (zxhjByqlqsqktjEntity.getNsrsbh() == null) {
                updateWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getNsrsbh);
            }else {
                updateWrapper.eq(ZxhjByqlqsqktjRglrEntity::getNsrsbh, zxhjByqlqsqktjEntity.getNsrsbh());
            }
            if (zxhjByqlqsqktjEntity.getNsrmc() == null) {
                updateWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getNsrmc);
            }else {
                updateWrapper.eq(ZxhjByqlqsqktjRglrEntity::getNsrmc, zxhjByqlqsqktjEntity.getNsrmc());
            }
            if (zxhjByqlqsqktjEntity.getJcajbh() == null) {
                updateWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getJcajbh);
            }else {
                updateWrapper.eq(ZxhjByqlqsqktjRglrEntity::getJcajbh, zxhjByqlqsqktjEntity.getJcajbh());
            }
            if (zxhjByqlqsqktjEntity.getZxry() == null) {
                updateWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getZxry);
            }else {
                updateWrapper.eq(ZxhjByqlqsqktjRglrEntity::getZxry, zxhjByqlqsqktjEntity.getZxry());
            }
            if (zxhjByqlqsqktjEntity.getAjszjgDm() == null) {
                updateWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getAjszjgDm);
            }else {
                updateWrapper.eq(ZxhjByqlqsqktjRglrEntity::getAjszjgDm, zxhjByqlqsqktjEntity.getAjszjgDm());
            }
            if (zxhjByqlqsqktjEntity.getRkxhrq() == null) {
                updateWrapper.isNull(ZxhjByqlqsqktjRglrEntity::getRkxhrq);
            }else {
                updateWrapper.eq(ZxhjByqlqsqktjRglrEntity::getRkxhrq, zxhjByqlqsqktjEntity.getRkxhrq());
            }
            ZxhjByqlqsqktjRglrEntity ZxhjByqlqsqktjRglrEntity = new ZxhjByqlqsqktjRglrEntity();
            BeanUtils.copyProperties(zxhjByqlqsqktjEntity, ZxhjByqlqsqktjRglrEntity);
            int update = zxhjByqlqsqktjRglrMapper.update(ZxhjByqlqsqktjRglrEntity, updateWrapper);
            if (update == 1) {
                return true;
            } else {
                return false;
            }
        }
    }

}